package com.LostAndFound.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.LostAndFound.domain.User;
import com.LostAndFound.service.UserListService;
import com.LostAndFound.utils.CommonsUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class UserListServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	UserListService service = new UserListService();
	
	// 分页
	public void getUserListForEasyUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 当前页
		int pageNumber = Integer.parseInt(request.getParameter("page"));
		// 每页总条数
		int pageSize = Integer.parseInt(request.getParameter("rows"));
		// 总记录数
		int total = service.getTotalUserForEasyUI();
		List<User> userList = service.getUserListForEasyUI(pageNumber,pageSize);
		Gson gson = new Gson();
		String json = gson.toJson(userList);
		// 遍历json格式的数组，添加修改字段
		JsonParser parser = new JsonParser();
		JsonElement jsonElement = parser.parse(json);
		JsonArray jsonArray = jsonElement.getAsJsonArray();
		for (int i = 0; i < jsonArray.size(); i++) {
			JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
			if(jsonObject.get("gender").getAsString().equals("1")) {
				jsonObject.remove("gender");
				jsonObject.addProperty("gender", "男");
				jsonObject.addProperty("genderNum", "1");
			}else {
				jsonObject.remove("gender");
				jsonObject.addProperty("gender", "女");
				jsonObject.addProperty("genderNum", "0");
			}
		}
		// 又变成json格式
		String rows = gson.toJson(jsonElement);
		
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"total\":"+total+",\"rows\":"+rows+"}");
	}
	// 删除用户的记录
	public void handleDeleteUserForEasyUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uid = request.getParameter("uid");
		boolean success = false;
		success = service.handleDeleteUserForEasyUI(uid);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	// 检查用户名是否可用
	public void checkAddUsername(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		boolean isUsed = false;
		isUsed = service.checkAddUsername(username);
		if(isUsed) {
			response.getWriter().write("false");
		}else {
			response.getWriter().write("true");
		}
	}
	// 添加用户
	public void handleAddUserEasyUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取数据
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String realname = request.getParameter("realname");
		String cellphone = request.getParameter("cellphone");
		Integer gender = Integer.parseInt(request.getParameter("gender"));
		String email = request.getParameter("email");
		String resume = request.getParameter("resume");
		// 封装数据
		User user = new User();
		user.setUid(CommonsUtils.getUUID());
		user.setUsername(username);
		user.setPassword(password);
		user.setRealname(realname);
		user.setCellphone(cellphone);
		user.setGender(gender);
		user.setEmail(email);
		user.setResume(resume);
		// 传递到 service 层
		boolean success = service.handleAddUserEasyUI(user);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	public void handleEditUserEasyUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取数据
		String uid = request.getParameter("uid");
		String password = request.getParameter("password");
		String realname = request.getParameter("realname");
		String cellphone = request.getParameter("cellphone");
		Integer gender = Integer.parseInt(request.getParameter("gender"));
		String email = request.getParameter("email");
		String resume = request.getParameter("resume");
		// 封装数据
		User user = new User();
		user.setUid(uid);
		user.setPassword(password);
		user.setRealname(realname);
		user.setCellphone(cellphone);
		user.setGender(gender);
		user.setEmail(email);
		user.setResume(resume);
		// 传递到 service 层
		boolean success = service.handleEditUserEasyUI(user);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
		
}